package com.xhs.main.controller.cwb;

import com.alibaba.fastjson.JSONObject;
import com.xhs.main.entity.cwb.CwbProposer;
import com.xhs.main.entity.cwb.CwbProposered;
import com.xhs.main.service.cwb.impl.CwbProposerServiceImpl;
import com.xhs.main.service.cwb.impl.CwbProposeredServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * create by ljw on 2020/10/19
 *
 */
@Controller
public class MoneyApplyController {

    Integer cwbid = 0;
    Integer agreeid = 0;
    Integer disagreeid = 0;


    @Autowired
    CwbProposerServiceImpl cwbProposerService;

    @Autowired
    CwbProposeredServiceImpl cwbProposeredService;

    //员工添加申请
    @RequestMapping("/addApply")
    public String addApply(HttpServletRequest request){
        CwbProposer cwbProposer = new CwbProposer();
        //取值
        String id = request.getParameter("employeeID");
        String applypeoson = request.getParameter("name");
        String department = request.getParameter("department");
        String applytype = request.getParameter("applytype");
        String reason = request.getParameter("reason");
        String leader = request.getParameter("leader");
        Double money = 0.0;
        if(request.getParameter("money") != null && !request.getParameter("money").equals("")){
            money = Double.parseDouble(request.getParameter("money"));
        }
        String bill = request.getParameter("bill");
        if(bill.equals("")){
            bill="无";
        }else{
            bill = request.getParameter("bill");
        }
        String date = request.getParameter("date");
        String status = request.getParameter("status");
        //赋值
        cwbProposer.setCwbPId(0);
        cwbProposer.setCwbPApplydate(date);
        cwbProposer.setCwbPMoney(money);
        cwbProposer.setCwbPLeader(leader);
        cwbProposer.setCwbPApplypeoson(applypeoson);
        cwbProposer.setCwbPStatus(status);
        cwbProposer.setCwbPDepartment(department);
        cwbProposer.setCwbPEmployeeid(id);
        cwbProposer.setCwbPApplytype(applytype);
        cwbProposer.setCwbPBill(bill);
        cwbProposer.setCwbPApplyreason(reason);
        cwbProposer.setCwbPShow("1");
        //插入数据
        cwbProposerService.addCwbProposer(cwbProposer);
        return "redirect:/#/page/cwb/cwbmoneyapplyopr.html";
    }

    //展示员工申请
    @RequestMapping("/showApply")
    @ResponseBody
    public Map<String,Object> showApply(Integer page, Integer limit){
        List<CwbProposer> cwbProposers = cwbProposerService.getAllCwbProposerByLimit((page-1)*limit,limit);
        List<CwbProposer> cwbProposerslist = cwbProposerService.getAllCwbProposer();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",cwbProposerslist.size());
        map.put("data",cwbProposers);
        return map;
    }

    //部门级联(获取部门信息然后判断生成什么主管)
    @RequestMapping("/getDepartment")
    @ResponseBody
    public List<String> getDepartment(String department){
        //存放部门主管
        List<String> people = new ArrayList<>();
        if(department != null){

            if(department.equals("人事部")){
                people.add("邵振山");
                people.add("伍佰");
            }else if(department.equals("财务部")){
                people.add("冷建伟");
                people.add("林子祥");
            }else if(department.equals("研发部")){
                people.add("李长顺");
                people.add("周华健");
            }else if(department.equals("生产部")){
                people.add("谭川东");
                people.add("花花的世界");
            }else if(department.equals("销售部")){
                people.add("韩家瑞");
                people.add("韩雪");
            }else if(department.equals("物资部")){
                people.add("张赛");
                people.add("高圆圆");
            }
        }
        return people;
    }

    //条件查询员工申请
    @RequestMapping("/showApplyByCondition")
    @ResponseBody
    public Map<String,Object> showApplyByCondition(@RequestParam("condition") String condition,Integer page,Integer limit){
        //获取前端传递的json数据
        JSONObject object = JSONObject.parseObject(condition);
        String employeeID = (String) object.get("employeeID");
        String people = (String) object.get("people");
        String applyType = (String) object.get("applyType");
        String department = (String) object.get("department");
        List<CwbProposer> cwbProposers = cwbProposerService.getAllProposerByCondition(employeeID,applyType,people,department,page,limit);
        List<CwbProposer> cwbProposerslist =  cwbProposerService.getAllCwbProposer();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",cwbProposerslist.size());
        map.put("data",cwbProposers);
        return map;
    }

    @RequestMapping("/delete")
    public String deleteApply(Integer id){
        if(id != null){
            cwbProposerService.updateOneID(id);
        }
        return "redirect:/#/page/cwb/cwbmoneyapplyopr.html";
    }

    //纯文本文件的字符编码未声明。
    // 如果该文件包含 US-ASCII 范围之外的字符，该文件将在某些浏览器配置中呈现为乱码。
    // 该文件的字符编码需要在传输协议层声明，或者在文件中加入一个 BOM（字节顺序标记）。
    //解决 produces = {"application/json;charset=UTF-8"}
    @RequestMapping("/edit")
    public String editApply(HttpServletRequest request){
        String id = request.getParameter("id");
        if(id != null && !"".equals(id)){
            cwbid = Integer.parseInt(id);
        }
        String reason = request.getParameter("reason");
        String bill = request.getParameter("bill");
        if(bill == ""){
            bill="无";
        }else{
            bill = request.getParameter("bill");
        }
        cwbProposerService.updateOneApply(reason,bill,cwbid);
        return "redirect:/#/page/cwb/cwbmoneyapplyopr.html";
    }

    //审批通过
    @RequestMapping("/agreeApply")
    public String agreeApply(HttpServletRequest request){
        System.out.println("agreeApply");

        agreeid = Integer.parseInt(request.getParameter("id"));
//        String reason = request.getParameter("reason");
        CwbProposer cwbProposer = cwbProposerService.getOneByID(agreeid);
        System.out.println(cwbProposer.getCwbPApplyreason());
        //经办人
        String oprpeople = "楚云飞";
        //审核人
        String approverPeople = "冷建伟";
        //时间转换
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String approoverDate = sim.format(date);
        //将审核过后的数据插入到已审核表中
        CwbProposered cwbProposered = new CwbProposered(0,cwbProposer.getCwbPEmployeeid(),
                cwbProposer.getCwbPApplytype(),cwbProposer.getCwbPApplyreason(),cwbProposer.getCwbPApplypeoson(),cwbProposer.getCwbPDepartment(),
                cwbProposer.getCwbPMoney(),"无","1",cwbProposer.getCwbPShow(),cwbProposer.getCwbPApplydate(),
                cwbProposer.getCwbPLeader(),approoverDate,oprpeople,approverPeople);
        cwbProposeredService.addCwbProposered(cwbProposered);
       return "cg";
    }

    //审批未通过
    @RequestMapping("/disAgreeApply")
    @ResponseBody
    public String disAgreeApply(HttpServletRequest request){
        System.out.println("disagreeApply");

        disagreeid = Integer.parseInt(request.getParameter("id"));
//        String reason = request.getParameter("reason");
        CwbProposer cwbProposer = cwbProposerService.getOneByID(disagreeid);
        System.out.println(cwbProposer.getCwbPApplyreason());
        //经办人
        String oprpeople = "楚云飞";
        //审核人
        String approverPeople = "冷建伟";
        //时间转换
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String approoverDate = sim.format(date);
        //将审核过后的数据插入到已审核表中
        CwbProposered cwbProposered = new CwbProposered(0,cwbProposer.getCwbPEmployeeid(),
                cwbProposer.getCwbPApplytype(),cwbProposer.getCwbPApplyreason(),cwbProposer.getCwbPApplypeoson(),cwbProposer.getCwbPDepartment(),
                cwbProposer.getCwbPMoney(),"无","0",cwbProposer.getCwbPShow(),cwbProposer.getCwbPApplydate(),
                cwbProposer.getCwbPLeader(),approoverDate,oprpeople,approverPeople);
        cwbProposeredService.addCwbProposered(cwbProposered);
        return "cg";
    }

    //审批过后的数据展示
    @RequestMapping("/showOprOkData")
    @ResponseBody
    public Map<String, Object> showOprData(Integer id,Integer page, Integer limit) {
        //修改显示状态
        if(id != null){
            cwbProposerService.updateOneID(id);
        }
        //展示的数据
        List<CwbProposered> cwbProposereds = cwbProposeredService.getAllCwbProposeredByLimit((page-1)*limit,limit);
        List<CwbProposered> cwbProposeredList = cwbProposeredService.getAllCwbProposered();
        Map<String, Object> datalist = new HashMap<>();
        datalist.put("code", 0);
        datalist.put("msg", "成功");
        datalist.put("count", cwbProposeredList.size());
        datalist.put("data", cwbProposereds);
        return datalist;
    }

    //审批后的条件查询员工申请(通过)
    @RequestMapping("/showApplyedByCondition")
    @ResponseBody
    public Map<String,Object> showApplyedByCondition(@RequestParam("condition") String condition,Integer page,Integer limit){
        //获取前端传递的json数据
        JSONObject object = JSONObject.parseObject(condition);
        String employeeID = (String) object.get("employeeID");
        String people = (String) object.get("people");
        String applyType = (String) object.get("applyType");
        String department = (String) object.get("department");
        List<CwbProposered> proposeredList = cwbProposeredService.getByCondition(employeeID, applyType, people, department, page, limit);
        List<CwbProposered> allCwbProposered = cwbProposeredService.getAllCwbProposered();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",allCwbProposered.size());
        map.put("data",proposeredList);
        return map;
    }

    //审批过后的数据展示(未通过)
    @RequestMapping("/showOprNoOkData")
    @ResponseBody
    public Map<String, Object> showOprNoOkData(Integer id,Integer page, Integer limit) {
        //修改显示状态
        if(id != null){
            cwbProposerService.updateOneID(id);
        }
        //展示的数据
        List<CwbProposered> cwbProposereds = cwbProposeredService.getAllNoCwbProposeredByLimit((page-1)*limit,limit);
        List<CwbProposered> cwbProposeredList = cwbProposeredService.getAllNoCwbProposered();
        Map<String, Object> datalist = new HashMap<>();
        datalist.put("code", 0);
        datalist.put("msg", "成功");
        datalist.put("count", cwbProposeredList.size());
        datalist.put("data", cwbProposereds);
        return datalist;
    }

    //审批后的条件查询员工申请(未通过)
    @RequestMapping("/showApplyedByNoCondition")
    @ResponseBody
    public Map<String,Object> showApplyedByNoCondition(@RequestParam("condition") String condition,Integer page,Integer limit){
        //获取前端传递的json数据
        JSONObject object = JSONObject.parseObject(condition);
        String employeeID = (String) object.get("employeeID");
        String people = (String) object.get("people");
        String applyType = (String) object.get("applyType");
        String department = (String) object.get("department");
        List<CwbProposered> proposeredList = cwbProposeredService.getByNoCondition(employeeID, applyType, people, department, page, limit);
        List<CwbProposered> allCwbProposered = cwbProposeredService.getAllNoCwbProposered();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",allCwbProposered.size());
        map.put("data",proposeredList);
        return map;
    }
}
